//
//  KoiAccountingUser.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 16.03.25.
//

import Foundation
struct KoiAccountingUser : Codable{
    var uid: String?
    var nickname: String?
    var email: String?
    var phone: String?
    var heading: String?
    var age: Int?
    var sex: Int?
    var country: String?
    var province: String?
    var city: String?
    var state: Int?
    var isTrial: Int?
    var hasPw: Int?
    var hasWechat: Int?
    var createTime: String?
    var updateTime: String?
    var accessToken: String?
    var refreshToken: String?
    var accessTokenExp: Double?
    var isInit: Int?
    var wxProfile:KoiWxProfile?
    
    enum CodingKeys: String, CodingKey {
            case uid = "uid"
            case nickname = "nickname"
            case email = "email"
            case phone = "phone"
            case heading = "heading"
            case sex = "sex"
            case age = "age"
            case province = "province"
            case country = "country"
            case city = "city"
            case state = "state"
            case isInit = "is_init"
            case isTrial = "is_trial"
            case hasPw = "has_pw"
            case hasWechat = "has_wechat"
            case createTime = "create_time"
            case updateTime = "update_time"
            case accessToken = "at"
            case refreshToken = "rt"
            case accessTokenExp = "at_exp"
            case wxProfile = "wx_profile"
        }
    
    static var defaultAvatar = URL(string: "https://img.koiaccounting.com/koi-default-avatar.png")!
    
    // 是游客登录
    func isTrialLogin() -> Bool {
        return isTrial == 1
    }
    
    func isNeedInit() -> Bool {
        return isInit == 0
    }
    
    func hasPassword() -> Bool {
        return hasPw == 1
    }
    
    func isBindPhone() -> Bool {
        return phone != nil
    }
    
    func isBindWeChat() -> Bool {
        return wxProfile != nil
    }
    
    func getNickname() -> String {
        return nickname ?? ""
    }
    
    func getGender() -> String {
        if sex == 1 {
            return "男"
        } else if sex == 2 {
            return "女"
        } else {
            return ""
        }
    }
    
    func getHeadImg() -> URL {
        var url: URL?
        if let imgUrl = heading ?? wxProfile?.headimgurl {
            url = URL(string: imgUrl)
        }
        return url ?? URL(string: "https://img.koiaccounting.com/koi-default-avatar.png")!
    }
    
    func getHidePhoneNumber() -> String {
        guard let phone = phone else {
            return ""
        }
        guard phone.count == 11 else {
            return ""
        }
        let start = phone.prefix(3) // 前三位
        let end = phone.suffix(4)   // 后四位
        return "\(start)****\(end)"
    }
}



extension Optional where Wrapped == KoiAccountingUser {
    func getHeadImg() -> URL {
        return self?.getHeadImg() ?? URL(string: "https://img.koiaccounting.com/koi-default-avatar.png")!
    }
}
